package com.ease.model;

import com.ease.common.util.JsonUtil;
import com.ease.model.criteria.Criteria;
import com.ease.model.criteria.CriteriaType;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class NamespaceModel implements Model, EnumerableModel {//

	/**
	 *
	 */
	private static final long serialVersionUID = -4329094139820431224L;

	public static final String Attribute_code = "code";

	/** id */
	protected long id = RAW_ID;

	/** name */
	protected String name;

	/** code */
	protected String code;

	/** version */
	protected long version = VERSION;

	/** memo */
	protected String memo;

	/** name like */
	protected String nameLike;

	public NamespaceModel() {
	}

	public static long idOf(Model model) {
		return (null == model) ? Model.RAW_ID : model.getId();
	}

	/**
	 * use namespace() instead of getNamespace() for escape from JSON marshaling
	 * 
	 * @return
	 */
	public String namespace() {
		return this.getClass().getSimpleName();
	}

	/**
	 * use beNew instead of isNew for escape from JSON marshaling
	 * 
	 * @return
	 */
	public boolean beNew() {
		return this.id <= 0;
	}

	public String toString() {
		return JsonUtil.marshal(this);
	}

	/**
	 * @param id
	 *            The id to set.
	 */
	public void setId(long id) {
		// if (!isNew()) {
		// throw new RuntimeException("this.id(=" + this.id +
		// ") cannot be changed!");
		// }
		this.id = id;
	}

	/**
	 * @return Returns the id.
	 */
	@Criteria(type = CriteriaType.PrimaryKey)
	public long getId() {
		return id;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return Returns the version.
	 */
	@Criteria(type = CriteriaType.Ignore)
	public long getVersion() {
		return version;
	}

	/**
	 * @param version
	 *            The version to set.
	 */
	public void setVersion(long version) {
		this.version = version;
	}

	/**
	 * @return Returns the memo.
	 */
	public String getMemo() {
		return memo;
	}

	/**
	 * @param memo
	 *            The memo to set.
	 */
	public void setMemo(String memo) {
		this.memo = memo;
	}

	// @Criteria(type = CriteriaType.Like, columnName = "code")
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the nameLike
	 */
	@Criteria(type = CriteriaType.Like, columnName = "name")
	public String getNameLike() {
		return nameLike;
	}

	/**
	 * @param nameLike
	 *            the nameLike to set
	 */
	public void setNameLike(String nameLike) {
		this.nameLike = nameLike;
	}
}
